summaryrefslogtreecommitdiffstats
path: root/src/core/hle/service/server_manager.h
blob: 5173ce46e1b0acce0d68f2373590d065279281b5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include <list>
#include <mutex>
#include <optional>
#include <vector>

#include "common/polyfill_thread.h"
#include "common/thread.h"
#include "core/hle/result.h"
#include "core/hle/service/hle_ipc.h"
#include "core/hle/service/os/multi_wait.h"
#include "core/hle/service/os/mutex.h"

namespace Core {
class System;
}

namespace Kernel {
class KEvent;
class KServerPort;
class KServerSession;
} // namespace Kernel

namespace Service {

class Port;
class Session;

class ServerManager {
public:
    explicit ServerManager(Core::System& system);
    ~ServerManager();

    Result RegisterSession(Kernel::KServerSession* session,
                           std::shared_ptr<SessionRequestManager> manager);
    Result RegisterNamedService(const std::string& service_name,
                                SessionRequestHandlerFactory&& handler_factory,
                                u32 max_sessions = 64);
    Result RegisterNamedService(const std::string& service_name,
                                std::shared_ptr<SessionRequestHandler>&& handler,
                                u32 max_sessions = 64);
    Result ManageNamedPort(const std::string& service_name,
                           SessionRequestHandlerFactory&& handler_factory, u32 max_sessions = 64);
    Result ManageDeferral(Kernel::KEvent** out_event);

    Result LoopProcess();
    void StartAdditionalHostThreads(const char* name, size_t num_threads);

    static void RunServer(std::unique_ptr<ServerManager>&& server);

private:
    void LinkToDeferredList(MultiWaitHolder* holder);
    void LinkDeferred();
    MultiWaitHolder* WaitSignaled();
    Result Process(MultiWaitHolder* holder);
    bool WaitAndProcessImpl();
    Result LoopProcessImpl();

    Result OnPortEvent(Port* port);
    Result OnSessionEvent(Session* session);
    Result OnDeferralEvent();
    Result CompleteSyncRequest(Session* session);

private:
    void DestroySession(Session* session);

private:
    Core::System& m_system;
    Mutex m_selection_mutex;

    // Events
    Kernel::KEvent* m_wakeup_event{};
    Kernel::KEvent* m_deferral_event{};

    // Deferred wait list
    std::mutex m_deferred_list_mutex{};
    MultiWait m_deferred_list{};

    // Guest state tracking
    MultiWait m_multi_wait{};
    Common::IntrusiveListBaseTraits<Port>::ListType m_servers{};
    Common::IntrusiveListBaseTraits<Session>::ListType m_sessions{};
    std::list<Session*> m_deferred_sessions{};
    std::optional<MultiWaitHolder> m_wakeup_holder{};
    std::optional<MultiWaitHolder> m_deferral_holder{};

    // Host state tracking
    Common::Event m_stopped{};
    std::vector<std::jthread> m_threads{};
    std::stop_source m_stop_source{};
};

} // namespace Service